<?php
/**
 * Created by PhpStorm.
 * User: this
 * Date: 2019/1/5
 * Time: 10:23
 */
namespace app\index\controller;
use app\index\model\Article as Art;
use think\Db;

class Article extends Base{
    /**
     * @throws \think\exception\DbException
     */
    public function index(){
        if(!$_GET){
            ajaxReturn(0,'参数错误！',0);
        }
        $pages = $this->getVal('show_num');
        $art_cate_id = $_GET['article_category_id'];
        $location = '百步园艺';
        switch ($art_cate_id){
            case 6:
                 $mark = 'lore';
                 $img = '/baibu/public/static/image/index/t6.png';
                 $desc = '养护知识';
                 break;
            default :
                $img = '/baibu/public/static/image/index/t6.png';
                $mark = 'news';
                $desc = '新闻资讯';
        }
        $article = new Art();
        $info = $article->getPage(array('article_category_id'=>$art_cate_id,'article_status'=>1),'article_id,article_title,article_author,article_abstract,article_edit_time,article_category_id,article_read_no',$pages);
        $page = '';
        if($info){
            foreach ($info as &$v){
                $v['article_edit_time'] = date("Y-m-d H:i:s",$v['article_edit_time']);
            }
            $page = $info->render();
        }
        $this->assign('article',$info);
        $this->assign('img',$img);
        $this->assign('desc',$desc);
        $this->assign('location',$location);
        $this->assign('page',$page);
        $this->assign('menu',json_encode($this->m));
        $this->assign('mark',$mark);
        $this->assign('cate_id',$art_cate_id);
        return $this->fetch('index');
    }

    /**
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail(){
        if(!$_GET){
            ajaxReturn(0,'参数错误!',0);
        }
        $art_id = $_GET['article_id'];
        $article = new Art();
        $art_info = $article->getOne(array('article_id'=>$art_id),'article_id,article_title,article_content,article_author,article_abstract,article_edit_time,article_category_id,article_read_no');
        //点击数量+1
        $article->increase(array('article_id'=>$art_id),'article_read_no');
        $location = '百步园艺';
        $type = 1;
        switch ($art_info['article_category_id']){
            case 1 :
                $mark = 'introduction';
                $img = '/baibu/public/static/image/index/t2.png';
                $desc = '关于我们';
                $type = 2;
                break;
            case 2 :
                $mark = 'lease';
                $img = '/baibu/public/static/image/index/t6.png';
                $desc = '植物租赁';
                $type = 2;
                break;
            case 3 :
                $mark = 'service';
                $img = '/baibu/public/static/image/index/t6.png';
                $desc = '服务项目';
                $type = 2;
                break;
            case 6 :
                $mark = 'lore';
                $img = '/baibu/public/static/image/index/t6.png';
                $desc = '养护知识';
                break;
            default:
                $img = '/baibu/public/static/image/index/t6.png';
                $mark = 'news';
                $desc = '新闻资讯';
        }
        $art_info['article_edit_time'] = date("Y-m-d H:i:s",$art_info['article_edit_time']);
        $this->assign('article',$art_info);
        $this->assign('img',$img);
        $this->assign('desc',$desc);
        $this->assign('location',$location);
        $this->assign('menu',json_encode($this->m));
        $this->assign('mark',$mark);
        if($type == 2){
            return $this->fetch('show');
        }else{
            return $this->fetch('news');
        }
    }
}